/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import Obliczenia.Obliczenia;

/**
 *
 * @author Darek
 */
public class Gui extends javax.swing.JFrame {


    ArrayList<JTextField> textFields = new ArrayList<JTextField>();
    Obliczenia obliczenia = new Obliczenia();
    /**
     * Creates new form GUI
     */
    public Gui() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tytulLabel = new javax.swing.JLabel();
        liczbaWierszyTextField = new javax.swing.JTextField();
        wierszeLabel = new javax.swing.JLabel();
        liczbaKolumnTextField = new javax.swing.JTextField();
        kolumnyLabel = new javax.swing.JLabel();
        tabelaPanel = new javax.swing.JPanel();
        rozwiazanieLabel = new javax.swing.JLabel();
        rozwiazanieScrollPane = new javax.swing.JScrollPane();
        rozwiazanieTextArea = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        plikMenu = new javax.swing.JMenu();
        zakonczMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tytulLabel.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        tytulLabel.setText("Teoria podejmowania decyzji");

        liczbaWierszyTextField.setText("7");
        liczbaWierszyTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        liczbaWierszyTextField.getDocument().addDocumentListener(new LiczbaWierszyTextFieldActionListener());
        
        wierszeLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        wierszeLabel.setText("wiersze");

        liczbaKolumnTextField.setText("7");
        liczbaKolumnTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        kolumnyLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        kolumnyLabel.setText("kolumny");
        liczbaKolumnTextField.getDocument().addDocumentListener(new LiczbaKolumnTextFieldActionListener());
        
        tabelaPanel.setSize(700, 480);
        tabelaPanel.setPreferredSize( new Dimension( 700, 480 ) );
        tabelaPanel.setMinimumSize( new Dimension( 700, 480 ) );
        
        initializeJPanelGridLayout(liczbaWierszyTextField, liczbaKolumnTextField);
        
        rozwiazanieLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        rozwiazanieLabel.setText("Rozwiązanie:");

        rozwiazanieTextArea.setColumns(20);
        rozwiazanieTextArea.setRows(5);
        rozwiazanieScrollPane.setViewportView(rozwiazanieTextArea);

        jButton1.setText("Rozwiąż");
        jButton1.addActionListener(new RozwiazButtonListener());
        
        plikMenu.setText("Plik");

        zakonczMenuItem.setText("Zakończ");
        zakonczMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zakonczMenuItemActionPerformed(evt);
            }
        });
        plikMenu.add(zakonczMenuItem);

        jMenuBar1.add(plikMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tytulLabel)
                .addGap(352, 352, 352))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tabelaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(285, 285, 285)
                        .addComponent(liczbaWierszyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(wierszeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(liczbaKolumnTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kolumnyLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rozwiazanieScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rozwiazanieLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tytulLabel)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(liczbaWierszyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wierszeLabel)
                    .addComponent(liczbaKolumnTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kolumnyLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(tabelaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rozwiazanieLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rozwiazanieScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void zakonczMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zakonczMenuItemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_zakonczMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
    
    public void initializeJPanelGridLayout(JTextField liczbaWierszyTextField, JTextField liczbaKolumnTextField){
    	tabelaPanel.removeAll();
    	GridLayout tabelaLayout = new GridLayout(Integer.parseInt(liczbaWierszyTextField.getText()), Integer.parseInt(liczbaKolumnTextField.getText()), 20, 20);
        tabelaPanel.setLayout(tabelaLayout);
        String [] tabela = {"0", "25", "18", "20", "0", "15", "10", "25", "0", "0", "0", "0", "30", "45", "18", "0", "0", "8", "10", "0", "20", "20", "0", "8", 
        		"0", "17", "0", "0", "0", "0", "10", "17", "0", "24", "0", "15", "30", "0", "0", "24", "0", "18", "10", "45", "20", "0", "0", "18", "0"};
        textFields.clear();
        if(Integer.parseInt(liczbaKolumnTextField.getText()) == 7 && Integer.parseInt(liczbaWierszyTextField.getText()) == 7){
        	for(int i = 0; i < 49 ; i++){
        		JTextField text = new JTextField(tabela[i]);
            	text.setHorizontalAlignment(SwingConstants.CENTER);
            	textFields.add(text);
        	}
        }
        else{
        	for(int i = 0; i < Integer.parseInt(liczbaKolumnTextField.getText()) * Integer.parseInt(liczbaWierszyTextField.getText()); i++){
            	JTextField text = new JTextField("0");
            	text.setHorizontalAlignment(SwingConstants.CENTER);
            	textFields.add(text);
            }
        }
        
        for(int i =0; i < textFields.size(); i++)
        	tabelaPanel.add(textFields.get(i));
        tabelaPanel.revalidate();
        tabelaPanel.repaint();
    }
    
    public ArrayList<Double> translacjaArrayListyJTextField(){
    	ArrayList<Double> wartosci = new ArrayList<Double>();
    	for( int i = 0; i < textFields.size(); i++){
    		wartosci.add(Double.parseDouble(textFields.get(i).getText()));
    	}
    	return wartosci;
    }
    
    public void warning(JTextField textField){
	    if (Integer.parseInt(textField.getText())<1 || Integer.parseInt(textField.getText())>10){
	        JOptionPane.showMessageDialog(null,
	                "Podaj liczbę z zakresu pomiędzy 1, a 10", "Wprowadzono złą wartość",
	                JOptionPane.ERROR_MESSAGE);
	    }    
	    else{
    		initializeJPanelGridLayout(liczbaWierszyTextField, liczbaKolumnTextField);
	    }
	}
    
    class LiczbaWierszyTextFieldActionListener implements DocumentListener{
    	@Override
    	public void changedUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub
    		warning(liczbaWierszyTextField);
    	}

    	@Override
    	public void insertUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub
    		warning(liczbaWierszyTextField);        		
    	}

    	@Override
    	public void removeUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub		
    	}	
    }

    class LiczbaKolumnTextFieldActionListener implements DocumentListener{
    	@Override
    	public void changedUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub
    		warning(liczbaKolumnTextField);
    	}

    	@Override
    	public void insertUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub
    		warning(liczbaKolumnTextField);        		
    	}

    	@Override
    	public void removeUpdate(DocumentEvent e) {
    		// TODO Auto-generated method stub		
    	}	
    }
    
    class RozwiazButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			rozwiazanieTextArea.append("Obliczenie najmniejszego kosztu: \n Połączenie 1-2 koszt: 25 \n Połączenie 1-3 koszt: 18\n Połączenie 1-6 koszt: 15\n Połączenie 1-7 koszt: 10\n Połączenie 3-4 koszt: 8\n Połączenie 3-5 koszt: 10 \n\n Łączny koszt: 76");
		}
    	
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel kolumnyLabel;
    private javax.swing.JTextField liczbaKolumnTextField;
    private javax.swing.JTextField liczbaWierszyTextField;
    private javax.swing.JMenu plikMenu;
    private javax.swing.JLabel rozwiazanieLabel;
    private javax.swing.JScrollPane rozwiazanieScrollPane;
    private javax.swing.JTextArea rozwiazanieTextArea;
    private javax.swing.JPanel tabelaPanel;
    private javax.swing.JLabel tytulLabel;
    private javax.swing.JLabel wierszeLabel;
    private javax.swing.JMenuItem zakonczMenuItem;
    // End of variables declaration//GEN-END:variables
}
